//
//  PayMentGoodAllTableViewCell.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/8/30.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "PayMentGoodAllTableViewCell.h"
#import "PaymentTableViewCell.h"


@interface PayMentGoodAllTableViewCell()<UITableViewDelegate,UITableViewDataSource>

Strong UITableView * tableView;

Strong UIView * footView;



//头部视图

Strong UIView * headerView;


Strong UIView * lineView;

Strong UIImageView * picImageView;

Strong UILabel * nameLabel;
Strong UILabel * countLabel;
Strong UILabel * moneyLabel;




@end
@implementation PayMentGoodAllTableViewCell

- (instancetype)initWithCellIdentifier:(NSString *)cellID{
    
    if (self = [super initWithCellIdentifier:cellID]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addSubview:self.tableView];
        
        self.markStr = @"close";
        
        
    }
    
    return self;
    
}

- (UIView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(90))];
        
        _headerView.backgroundColor = getColor(whiteColor);
        
        _picImageView = [UIImageView new];
        _picImageView.image = [UIImage imageNamed:@"shanhaihuilogo"];
        _picImageView.layer.masksToBounds = YES;
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"三只松鼠,三只松鼠,三只松鼠";
        _nameLabel.font = DEF_FontSize_13;
        _nameLabel.textColor = getColor(@"323232");
        _nameLabel.numberOfLines = 0;
        
        _countLabel = [[UILabel alloc] init];
        _countLabel.text = @"X10";
        _countLabel.textColor = getColor(@"323232");
        _countLabel.font = DEF_FontSize_13;
        
        _moneyLabel = [[UILabel alloc] init];
        _moneyLabel.textColor = getColor(@"323232");
        _moneyLabel.text=@"49";
        _moneyLabel.font = DEF_FontSize_13;
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = getColor(dividerColor);
        
        [_headerView sd_addSubviews:@[self.picImageView,self.nameLabel,self.countLabel,self.moneyLabel,self.lineView]];
        
        _spreadBtn = [[CLCustomButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH  - 55,  CGRectGetMaxY(_moneyLabel.frame) + 50, 40,40) type:CLCustomButtonLeftImageType imageSize:CGSizeMake(16,8) midmargin:5];
        _spreadBtn.isShowSelectBackgroudColor = NO;
        _spreadBtn.imageView.image = [UIImage imageNamed:@"zhankai"];
        _spreadBtn.titleLabel.text = @"展开";
        _spreadBtn.titleLabel.textColor = getColor(@"CCCCCC");
        _spreadBtn.titleLabel.font = DEF_FontSize_11;
        _spreadBtn.backgroundColor = [UIColor whiteColor];

        [self.spreadBtn touchAction:^(CLCustomButton * _Nonnull button) {

        }];
        
        _spreadSoonBtn = [[UIButton alloc] init];
        [_spreadSoonBtn addTarget:self action:@selector(spreadSoonBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _spreadSoonBtn.frame = CGRectMake(SCREEN_WIDTH  - 55,  CGRectGetMaxY(_moneyLabel.frame) + 50, 40,40);

        
        [_headerView addSubview:self.spreadBtn];
        
        [_headerView addSubview:self.spreadSoonBtn];
        
        ws(self)
        [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(15));
            make.left.mas_equalTo(UNIT_WIDTH(15));
            make.width.mas_equalTo(UNIT_WIDTH(40));
            make.height.mas_equalTo(UNIT_HEIGHT(40));
        }];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakself.picImageView.mas_right).offset(UNIT_WIDTH(10));
            make.top.mas_equalTo(UNIT_WIDTH(15));
            make.width.mas_equalTo(UNIT_WIDTH(151));
        }];
        
        [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakself.nameLabel.mas_right).offset(30);
            make.top.mas_equalTo(UNIT_WIDTH(15));
            make.width.mas_equalTo(UNIT_WIDTH(35));
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(UNIT_WIDTH(-15));
            make.top.mas_equalTo(UNIT_WIDTH(15));
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            //make.top.mas_equalTo(weakself.picImageView.mas_bottom).offset(UNIT_HEIGHT(20));
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(0);
        }];
        
        
    }
    
    return _headerView;
}

- (UIView *)footView{
    
    if (!_footView) {
        
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(50))];
        
        _closeBtn = [[CLCustomButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH  - 55, UNIT_HEIGHT(10), UNIT_HEIGHT(35),UNIT_HEIGHT(35)) type:CLCustomButtonLeftImageType imageSize:CGSizeMake(16,8) midmargin:5];
        _closeBtn.isShowSelectBackgroudColor = NO;
        _closeBtn.imageView.image = [UIImage imageNamed:@"zhankai"];
        _closeBtn.titleLabel.text = @"收起";
        _closeBtn.titleLabel.textColor = getColor(@"CCCCCC");
        _closeBtn.titleLabel.font = DEF_FontSize_11;
        _closeBtn.backgroundColor = [UIColor whiteColor];
        
        [self.closeBtn touchAction:^(CLCustomButton * _Nonnull button) {
   
        }];
        
        
        _closeSoonBtn = [[UIButton alloc] init];
        [_closeSoonBtn addTarget:self action:@selector(closeSoonBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _closeSoonBtn.frame = CGRectMake(SCREEN_WIDTH  - 55, UNIT_HEIGHT(10), UNIT_HEIGHT(35),UNIT_HEIGHT(35));

        [_footView addSubview:self.closeBtn];
        [_footView addSubview:self.closeSoonBtn];
 
    }
    
    return _footView;
    
}

- (void)spreadSoonBtnClick:(UIButton *)sender{
    
    self.markStr = @"spread";
    
    _tableView.tableFooterView = self.footView;
    
    self.spreadBtn.hidden = YES;
    
    self.height = UNIT_HEIGHT(95) + self.cardArr.count  * UNIT_HEIGHT(150) + UNIT_HEIGHT(50);
    
    if (_getSpreadClick) {
        
        _getSpreadClick(self,self.markStr,sender,self.height);
    }
    
  //  [self.tableView reloadData];

}

- (void)closeSoonBtnClick:(UIButton *)sender{
    
    self.markStr = @"close";

    self.spreadBtn.hidden = NO;
    
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    
    if (_getCloseClick) {
        
         self.height = UNIT_HEIGHT(95);
        
        _getCloseClick(self,self.markStr,sender,self.height);
    }
    
    
   // [self.tableView reloadData];
    
}


//tabelView
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(100)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(whiteColor);
    }
    return _tableView;
}
#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    return self.cardArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PaymentCardTableViewCell * cell = [PaymentCardTableViewCell cellForTableView:tableView];
    
    if ([self.markStr isEqualToString:@"spread"]) {
    
        
        cell.model = self.cardArr[indexPath.row];
        
        cell.hidden = NO;
        
    }else{
        
        cell.hidden = YES;
    }
  
    
    cell.getchangeCardBtnClick = ^{
        
        SpphModel * model = self.cardArr[indexPath.row];
        
        if (_getchangeCardBtnClick) {
            _getchangeCardBtnClick(self,model.fSpbh);
        }
        
          
        
    };
    
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if ([self.markStr isEqualToString:@"spread"]) {
        
        return UNIT_HEIGHT(150);
        
    }else{
        
        return 0;
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


- (void)setCardArr:(NSMutableArray *)cardArr{
    
    _cardArr = cardArr;
    
   
  
//    if ([self.markStr isEqualToString:@"close"]) {
//
//         _tableView.height = UNIT_HEIGHT(95);
//
//         _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
//
//    }else{
//
//         _tableView.height = UNIT_HEIGHT(95) + self.cardArr.count  * UNIT_HEIGHT(150) + UNIT_HEIGHT(50);
//    }
//
   // [self.tableView reloadData];
}

- (void)setModel:(PayMentGoodModel *)model{
    
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:model.imagepfilename]];
    self.nameLabel.text = model.spmc;
    self.countLabel.text = [NSString stringWithFormat:@"×%@",model.xssl];
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",model.xsje];
    
   // model.isOpening = self.markStr;
    
   // NSLog(@"model.isOpeningcell %@",model.isOpening);
    
    if (model.isOpening) {
        
        _tableView.height = UNIT_HEIGHT(95);
        
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        
    }else{
        
        _tableView.height = UNIT_HEIGHT(95) + self.cardArr.count  * UNIT_HEIGHT(150) + UNIT_HEIGHT(50);
    }
    
    
    [self.tableView reloadData];
    
}


//-(CGFloat)cellHeight:(NSDictionary *)dic{
//    
//    
//    
//}


@end
